package com.itheima.health.service.impl;

import com.alibaba.excel.EasyExcel;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.ExcelDTO;
import com.itheima.health.dto.OrderSettingDTO;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.listener.MyExcelListener;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.vo.OrderSettingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预约ServiceImpl
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public List<OrderSettingVO> getOrderSettingByMonth(String year, String month) {
        // 创建YearMonth对象
        YearMonth yearMonth = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month));

        // 获取该月的第一天以及最后一天
        LocalDate firstDay = yearMonth.atDay(1);
        LocalDate lastDay = yearMonth.atEndOfMonth();

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String start = firstDay.format(formatter);
        String end = lastDay.format(formatter);
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);

        List<OrderSetting> data = orderSettingMapper.getOrderSettingByDate(startDate, endDate);
        List<OrderSettingVO> collect = data.stream().map(x -> {
            Date orderDate = x.getOrderDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderDate); // 设置Calendar的时间为Date对象的时间
            Integer day = calendar.get(Calendar.DAY_OF_MONTH);

            return new OrderSettingVO(day, x.getNumber(), x.getReservations());
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 上传文件
     * @param excelFile
     */
    @Override
    public void upload(MultipartFile excelFile) {
        if (!excelFile.isEmpty()){
            try {
                InputStream is = excelFile.getInputStream();
                //easyexcel读行数据
                EasyExcel.read(is, ExcelDTO.class, new MyExcelListener(orderSettingMapper))
                        .sheet(0)
                        .doRead();
                is.close();
                //批量插入后, 重新加载缓存, 能走到这里说明分类下的菜品大概率变化了, 防止缓存击穿
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            //文件为空
            throw new BusinessRuntimeException(MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }


    /**修改可预约数量
     *
     * @param dto
     */
    @Override
    public void editNumberByDate(OrderSettingDTO dto) {
        Integer count = orderSettingMapper.selectCountByDate(dto.getOrderDate());
        OrderSetting build = OrderSetting.builder().orderDate(dto.getOrderDate()).number(dto.getNumber()).build();

        if (count == 0){
            ArrayList<OrderSetting> orderSettings = new ArrayList<>();
            orderSettings.add(build);
            orderSettingMapper.insertBeatch(orderSettings);
        }else {
            orderSettingMapper.updateOrderSetting(build);
        }
    }
}
